import re

import numpy as np
import sklearn.preprocessing as sp
from matplotlib import pyplot as plt
from scipy import signal

path = r"E:\A大论文\论文数据\V图数据\挑选好的\20210423080000\NGNH232104230800.UD1"



def graphs(path):
    i = 0;
    gals = 0;
    g_flag=[];
    lines=[];
    for line in open(path):
        i = i + 1;
        # 筛选出gal
        if (i == 14):
            gal = re.findall(r'\d*', line)
            # print(re.findall(r'\d*', line))
            g_flag.clear();
            for g in gal:
                if g !='':
                    g_flag.append(float(g));
            gals = g_flag[0]/g_flag[1]
            # print(gals)
        if i >=18:
            # 分割字符串
            line = line.split(' ')
            # 取出每一行字符转为数字 [-2562.0, -2562.0]
            for li in line[:-1]:
                if li !='':
                    lines.append(float(li)*gals)
    #   得到文件的所有地震数据lines
    # print(lines)
    # liness = lines
    # 进行基线校正
    liness= signal.detrend(lines)
    # print(liness)
    #  画图
    x = np.linspace(0,((0+len(liness)-1)*(1/100)),len(liness))
    # 不显示图片
    # matplotlib.use('Agg')
    # 图片名
    # ax = plt.gca()
    # plt.text(0,1, path, fontsize=10, color='green', transform=ax.transAxes)
    plt.plot(x,liness)
    # 文件名
    # file_name = path.split("\\")[-1].split('.')[0] + path.split("\\")[-1].split('.')[1]
    # plt.savefig(r'./fig/{}'.format(file_name))
    plt.show()
    # plt.close()
graphs(path)
